﻿using System;
using WiMo.Games.Drawables;
using WiMo.Games.Drawing.Silverlight;
using System.Windows.Media;
using System.Windows.Controls;

namespace WiMo.Games.Silverlight.Drawing.Drawables
{
    public class SpriteShape  : SpriteShapeBase
    {
        public static SpriteShape CreateRectangle(Rectangle rect, Vector2 scaleFactor, Color lineColor, int width)
        {
            return CreateRectangle(rect, scaleFactor, lineColor, width, Origin.Center);
        }

        public static SpriteShape CreateRectangle(Rectangle rect, Vector2 scaleFactor, Color fillColor, Color lineColor, int width)
        {
            return CreateRectangle(rect, scaleFactor, fillColor, lineColor, width, Origin.Center);
        }

        public static SpriteShape CreateRectangle(Rectangle rect, Vector2 scaleFactor, Color lineColor, int width, Origin origin)
        {
            return CreateRectangle(rect, scaleFactor, Color.Transparent, lineColor, width, Origin.Center);
        }

        public static SpriteShape CreateRectangle(Rectangle rect, Vector2 scaleFactor, Color fillColor, Color lineColor, int width, Origin origin)
        {
            var shape = new SpriteShape();
            shape.Scale = scaleFactor;
            shape.ShapeType = ShapeType.Rectangle;
            shape.Origin = origin;
            shape.Rect = rect;
            shape.BorderColor = lineColor;
            shape.BorderWidth = width;
            shape.FillColor = fillColor;
            shape.IsTransient = true;

            var element = new System.Windows.Shapes.Rectangle();
            element.Width = shape.Size.Width;
            element.Height = shape.Size.Height;

            if (fillColor != Color.Transparent)
            {
                element.Stroke = new System.Windows.Media.SolidColorBrush(lineColor.GetMediaColor());
                element.StrokeThickness = width;
            }

            if (lineColor != Color.Transparent)
                element.Fill = new System.Windows.Media.SolidColorBrush(fillColor.GetMediaColor());

            TransformGroup tg = new TransformGroup();
            var translate = new TranslateTransform();
            var rotate = new RotateTransform();
            var scale = new ScaleTransform();

            tg.Children.Add(translate);
            tg.Children.Add(rotate);
            tg.Children.Add(scale);

            element.RenderTransform = tg;
            if (origin == Origin.Center)
            {
                element.SetValue(Canvas.TopProperty, (double)(shape.Position.Y - (shape.Size.Height / 2)));
                element.SetValue(Canvas.LeftProperty, (double)(shape.Position.X - (shape.Size.Width / 2)));
            }
            else
            {
                element.SetValue(Canvas.TopProperty, (double)(shape.Position.Y));
                element.SetValue(Canvas.LeftProperty, (double)(shape.Position.X));
            }

            shape.DrawInformation = element;

            return shape;
        }

        public static SpriteShape CreateCircle(PointF position, Vector2 scaleFactor, int radius, Color borderColor, int width, Origin origin)
        {
            var shape = new SpriteShape();
            shape.Scale = scaleFactor;
            shape.ShapeType = ShapeType.Ellipse;
            shape.IsTransient = true;

            var ellipse = new System.Windows.Shapes.Ellipse();

            if (borderColor != Color.Transparent)
            {
                ellipse.Stroke = new System.Windows.Media.SolidColorBrush(borderColor.GetMediaColor());
                ellipse.StrokeThickness = width;
            }

            ellipse.Width = radius;
            ellipse.Height = radius;

            if (origin == WiMo.Games.Drawables.Origin.Center)
            {
                ellipse.SetValue(Canvas.TopProperty, (double)(position.Y - (radius / 2)));
                ellipse.SetValue(Canvas.LeftProperty, (double)(position.X - (radius / 2)));
            }
            else
            {
                ellipse.SetValue(Canvas.TopProperty, (double)(position.Y - radius));
                ellipse.SetValue(Canvas.LeftProperty, (double)(position.X - radius));
            }

            shape.DrawInformation = ellipse;

            return shape;
        }

        public static SpriteShape CreateCircle(PointF position, Vector2 scaleFactor, int radius, Color borderColor, int width)
        {
            return CreateCircle(position, scaleFactor, radius, borderColor, width, Origin.Center);
        }

        public override void Render(IDisplay display)
        {
            display.Render(this);
        }
    }
}
